home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / id-utils.2 / id-utils / id-utils-3.2 / src / fnid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-09  |  4.5 KB  |  193 lines

  1. /* fnid.c -- report which files constitute an ID database
  2.    Copyright (C) 1996 Free Software Foundation, Inc.
  3.    Written by Greg McGary <gkm@gnu.ai.mit.edu>
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  18.  
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <getopt.h>
  22. #include "xfnmatch.h"
  23. #include "xstring.h"
  24. #include "xmalloc.h"
  25. #include "xnls.h"
  26. #include "idfile.h"
  27. #include "pathmax.h"
  28. #include "error.h"
  29. #include "xalloca.h"
  30.  
  31. void scan_files __P((struct idhead *idhp));
  32. void scan_member_file __P((struct member_file const *member));
  33. void usage __P((void));
  34.  
  35. char const *program_name;
  36. int show_version = 0;
  37. int show_help = 0;
  38. struct idhead idh;
  39. struct file_link *cw_dlink;
  40. int no_id_flag = 1;
  41.  
  42. /* How shall we separate file names?  */
  43.  
  44. enum separator_style separator_style = ss_contextual;
  45.  
  46. void
  47. usage (void)
  48. {
  49.   fprintf (stderr, _("Try `%s --help' for more information.\n"),
  50.        program_name);
  51.   exit (1);
  52. }
  53.  
  54. static struct option const long_options[] =
  55. {
  56.   { "file", required_argument, 0, 'f' },
  57.   { "separator", required_argument, 0, 'S' },
  58.   { "help", no_argument, &show_help, 1 },
  59.   { "version", no_argument, &show_version, 1 },
  60.   { 0 }
  61. };
  62.  
  63. static void
  64. help_me (void)
  65. {
  66.   printf (_("\
  67. Usage: %s [OPTION]... [PATTERN]...\n\
  68. "), program_name);
  69.  
  70.   printf (_("\
  71. Print constituent file names that match PATTERN,\n\
  72. using shell-style wildcards.\n\
  73.   -f, --file=FILE        file name of ID database\n\
  74.   -S, --separator=STYLE  STYLE is one of `braces', `space' or `newline'\n\
  75.       --help             display this help and exit\n\
  76.       --version          output version information and exit\n\
  77. "));
  78.   exit (0);
  79. }
  80.  
  81. int
  82. main (int argc, char **argv)
  83. {
  84.   program_name = argv[0];
  85.   idh.idh_file_name = 0;
  86.  
  87.   /* Set locale according to user's wishes.  */
  88.   setlocale (LC_ALL, "");
  89.  
  90.   /* Tell program which translations to use and where to find.  */
  91.   bindtextdomain (PACKAGE, LOCALEDIR);
  92.   textdomain (PACKAGE);
  93.  
  94.   for (;;)
  95.     {
  96.       int optc = getopt_long (argc, argv, "f:S:",
  97.                   long_options, (int *) 0);
  98.       if (optc < 0)
  99.     break;
  100.       switch (optc)
  101.     {
  102.     case 0:
  103.       break;
  104.  
  105.     case 'f':
  106.       idh.idh_file_name = optarg;
  107.       break;
  108.  
  109.     case 'S':
  110.       separator_style = parse_separator_style (optarg);
  111.       break;
  112.  
  113.     default:
  114.       usage ();
  115.     }
  116.     }
  117.  
  118.   if (show_version)
  119.     {
  120.       printf ("%s - %s\n", program_name, PACKAGE_VERSION);
  121.       exit (0);
  122.     }
  123.  
  124.   if (show_help)
  125.     help_me ();
  126.  
  127.   if (separator_style == ss_contextual)
  128.     {
  129.       if (isatty (STDOUT_FILENO))
  130.     separator_style = DEFAULT_SEPARATOR_STYLE;
  131.       else
  132.     separator_style = ss_newline;
  133.     }
  134.  
  135.   argc -= optind;
  136.   argv += optind;
  137.   if (argc == 0)
  138.     {
  139.       static char *star = (char *) "*";
  140.       argc = 1;
  141.       argv = ☆
  142.     }
  143.  
  144.   /* Look for the ID database up the tree */
  145.   idh.idh_file_name = locate_id_file_name (idh.idh_file_name);
  146.   if (idh.idh_file_name == 0)
  147.     error (1, errno, _("can't locate `ID'"));
  148.  
  149.   init_idh_obstacks (&idh);
  150.   init_idh_tables (&idh);
  151.  
  152.   cw_dlink = get_current_dir_link ();
  153.   {
  154.     struct file_link **members = read_id_file (idh.idh_file_name, &idh);
  155.     struct file_link **members_N = &members[idh.idh_files];
  156.     struct file_link **flinkv_0 = MALLOC (struct file_link *, idh.idh_files + 1);
  157.     struct file_link **flinkv = flinkv_0;
  158.     char **patv_0 = MALLOC (char *, argc * 2);
  159.     char **patv_N;
  160.     char **patv = patv_0;
  161.  
  162.     for ( ; argc; argc--, argv++)
  163.       {
  164.     char *arg = *argv;
  165.     *patv++ = arg;
  166.     if (*arg != '*' && *arg != '/')
  167.       {
  168.         char *pat = MALLOC (char, strlen (arg) + 2);
  169.         sprintf (pat, "*/%s", arg);
  170.         *patv++ = pat;
  171.       }
  172.       }
  173.     patv_N = patv;
  174.  
  175.     for ( ; members < members_N; members++)
  176.       {
  177.     char *file_name = ALLOCA (char, PATH_MAX);
  178.     maybe_relative_file_name (file_name, *members, cw_dlink);
  179.     for (patv = patv_0; patv < patv_N; patv++)
  180.       {
  181.         if (fnmatch (*patv, file_name, MAYBE_FNM_CASEFOLD) == 0)
  182.           {
  183.         *flinkv++ = *members;
  184.         break;
  185.           }
  186.       }
  187.       }
  188.     *flinkv = 0;
  189.     print_filenames (flinkv_0, separator_style);
  190.   }
  191.   return 0;
  192. }
  193.